package GUI.Ventas;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;

import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.TableItem;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Ventas.Facturacion.CerrarVenta;
import article.entities.ArticleVO;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class VentasAbiertasMostrador extends Dialog {

	protected Object result;
	protected Shell shell;
	int opcion;
	private Table table;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VentasAbiertasMostrador(Shell parent, int style, int op) {
		super(parent, style);
		setText("SWT Dialog");
		opcion=op;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException, NoSalesException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException, NoSalesException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(533, 308);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0, 38);
		fd_table.left = new FormAttachment(0, 38);
		fd_table.right = new FormAttachment(100, -38);
		fd_table.bottom = new FormAttachment(0, 208);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(188);
		tblclmnNewColumn.setText("Articulos");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(51);
		tblclmnNewColumn_1.setText("A Pagar");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(170);
		tblclmnNewColumn_2.setText("Especificaciones");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("SaleKey");
		
		Button btnAgregarleArticulos = new Button(shell, SWT.NONE);
		btnAgregarleArticulos.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		
		FormData fd_btnAgregarleArticulos = new FormData();
		fd_btnAgregarleArticulos.left = new FormAttachment(table, -117);
		fd_btnAgregarleArticulos.top = new FormAttachment(table, 17);
		fd_btnAgregarleArticulos.right = new FormAttachment(table, 0, SWT.RIGHT);
		
		
		btnAgregarleArticulos.setLayoutData(fd_btnAgregarleArticulos);
		if(opcion ==0){
		btnAgregarleArticulos.setText("Agregarle Articulos");
		}
		if (opcion ==2){
			btnAgregarleArticulos.setText("Cerrar esta Venta");
		}
		SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
		final SaleVO [] sales = uso.getOpenTakeoOutSales();
		//System.out.println("#ventas" + sales.length);
		for(int i = 0; i< sales.length;i++){
		
			String m = "";
			TableItem tableItem = new TableItem(table, SWT.NONE);
			double precio=sales[i].getTotal();
			ArrayList <OrderVO> ords = sales[i].getOrders();
			//System.out.println("#ordenes" + sales[i].getOrders().size());
			for(int k=0; k< ords.size();k++){
				
				ArrayList <ArticleVO> arts = ords.get(k).getArts();
			for(int j=0;j<arts.size();j++){
				m=m+ " " + arts.get(j).getName();
				if(ords.get(k).getEspecif()!=null){
				tableItem.setText(ords.get(k).getEspecif().get(j));
			}
				else{
					tableItem.setText("no hay espc");
				}
				}
			
			tableItem.setText(0, m);
			tableItem.setText(1, String.valueOf(precio));
			tableItem.setText(3,String.valueOf(sales[i].getSaleKey()));
			
		}
		btnAgregarleArticulos.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(opcion == 0){
				SaleVO sale = null;
				TableItem[] items = table.getSelection();
				for(int i=0;i<sales.length;i++){
					if(items[0].getText(3).equalsIgnoreCase(String.valueOf(sales[i].getSaleKey()))){
					 sale = sales[i];
					}
				}
				AgregarArticulosDelivery a = new AgregarArticulosDelivery (shell, shell.getStyle(), sale,-2, 4, null);
				shell.setVisible(false);
				try {
					a.open();
					shell.close();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}}
				
				if(opcion ==2){
					SaleVO sale = null;
					SaleVO [] sales= new SaleVO[0];
					try {
						sales = ServiceFacade.getInstance().getSaleProxyMgt().getOpenTakeoOutSales();
					} catch (RemoteException e2) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e2.printStackTrace();
					} catch (ProxyException e2) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e2.printStackTrace();
					} catch (NoSalesException e2) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "No hay ventas abiertas de delivery");
						ventana.open();
						shell.dispose();
						e2.printStackTrace();
					}
					TableItem[] items = table.getSelection();
					for(int i=0;i<sales.length;i++){
						if(items[0].getText(3).equalsIgnoreCase(String.valueOf(sales[i].getSaleKey()))){
						 sale = sales[i];
						}
					}
				System.out.println("a ver" + 	sale.getOrders().size());
					CerrarVenta cerrar = new CerrarVenta (shell, shell.getStyle(), sale, null);
					shell.setVisible(false);
					try {
						cerrar.open();
						shell.close();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (EmptySaleExcpetion e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			}
		});
		}}}
